package com.tuanzi.loan.schedule.receipt;

import com.base.common.log.LogService;
import com.starbaba.tools.redis.RedisKey;
import com.tuanzi.loan.business.entity.product.product.LoanProductConsumeRecord;
import com.tuanzi.loan.business.entity.product.product.LoanProductInfo;
import com.tuanzi.loan.business.entity.product.product.LoanProductRechargeRecord;
import com.tuanzi.loan.business.repository.product.product.LoanProductRechargeRecordRepository;
import com.tuanzi.loan.business.service.product.product.LoanProductConsumeRecordService;
import com.tuanzi.loan.business.service.product.product.LoanProductInfoService;
import com.tuanzi.loan.business.service.product.product.impl.LoanProductInfoServiceImpl;
import com.tuanzi.loan.business.service.statistics.LoanProductClickUvService;
import com.tuanzi.loan.core.aspect.Debug;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * @Auther: hequnayu
 * @Date: 2019/7/16 10:28
 * @Description: 记录昨天一天的的甲方消费记录
 */
@Debug
@Slf4j
@Service
public class StaticProductPrepeyBalance {

    @Autowired
    private LoanProductInfoService infoService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Scheduled(cron = "${cron.product-prepay-task}")
    @Transactional(propagation= Propagation.NOT_SUPPORTED)
    public void statistic() {
        String cacheHadOpenPrepay = LoanProductInfoServiceImpl.PRODUCT_HAD_OPEN_PERPAY_SWITCH_KEY + LocalDate.now().minusDays(1).toString(); //获取昨天开启过预付下线的甲方
        Set<String> openPrepaySwitchIds = stringRedisTemplate.opsForSet().members(cacheHadOpenPrepay);
        LogService.info("记录甲方预付消费金额开始，甲方数量： " + openPrepaySwitchIds.size());
        for (String id : openPrepaySwitchIds){
            try{
                long proid = Long.parseLong(id);
                infoService.saveYesterdayConsumeMoney(proid); //保存消费记录
            }catch (Exception e){
                LogService.info("记录甲方预付消费金额出错");
            }
        }
        stringRedisTemplate.delete(LoanProductInfoServiceImpl.PRODUCT_HAD_OPEN_PERPAY_SWITCH_KEY);
        LogService.info("记录甲方预付消费金额结束");
    }


}
